package com.party.admin.web.controller.cosapi;

import com.party.file.cosapi.sign.Sign;
import com.party.common.utils.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * 腾讯云文件存储控制器
 * Created by wei.li
 *
 * @date 2017/10/16 0016
 * @time 10:21
 */

@Controller
@RequestMapping(value = "/cos/api")
public class CosController {

    public static final int APP_ID = 10052192;
    public static final String SECRET_ID = "AKID0zLb467fRifqb0rsyo5tVJKZqKiebe5B";
    public static final String SECRET_KEY = "UcfDJpkyGi90IDw0YsnxxUcLkWopbrC6";
    public static final String remotePath = "/userfiles/undefine/";
    public static final String bucketName = "tongxingzhe";
    public static final String signType1 = "appSign";
    public static final String signType2 = "appSign_once";


    /**
     * 获取签名信息
     * @param sign_type 签名类型
     * @param expired 多次签名时长
     * @param path 上传路径 + 文件名
     * @param bucketName bucket
     * @return 签名信息
     */
    @ResponseBody
    @RequestMapping(value = "/getAppSign")
    public String getAppSign(String sign_type, Long expired, String path, String bucketName){
        if(signType1.equals(sign_type))
        {
            return getAppSignature(expired, bucketName);
        }
        else if(StringUtils.isBlank(path))
        {
            return getAppSignatureNullParam(null);
        }
        return getAppSignatureOnce(path, bucketName);
    }


    /**
     * 获取签名信息
     * @param expiredTime 当前系统时间
     * @param bucket bucket
     * @return 签名
     */
    @ResponseBody
    @RequestMapping(value = "getAppSignature")
    public String getAppSignature(long expiredTime, String bucket){
        if(StringUtils.isBlank(bucket)) //前端传入的bucket为空字符串，则设置为默认的cos bucket
        {
            bucket = bucketName;
        }

        JSONObject json = new JSONObject();
        String sign = null;
        try {
            sign = Sign.appSignature(APP_ID, SECRET_ID, SECRET_KEY, expiredTime, bucket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != sign)
        {
            HashMap<String, String> updateData = new HashMap<>();
            updateData.put("sign",sign);
            json.put("message","sign success");
            json.put("code",-1);
            json.put("data",updateData);
        }
        else
        {
            json.put("success","sign fail");
            json.put("code",100);

        }
        return json.toString();
    }


    /**
     * 获取签名
     * @param remoteFilePath 上传目录
     * @param bucket bucket
     * @return 签名信息
     */
    @ResponseBody
    @RequestMapping(value = "getAppSignatureOnce")
    public String getAppSignatureOnce(String remoteFilePath, String bucket) {
        if(StringUtils.isBlank(remoteFilePath))
        {
            remoteFilePath = remotePath;
        }
        if(StringUtils.isBlank(bucket)) //前端传入的bucket为空字符串，则设置为默认的cos bucket
        {
            bucket = bucketName;
        }

        JSONObject json = new JSONObject();
        String sign = null;
        try {
            sign = Sign.appSignatureOnce(APP_ID, SECRET_ID, SECRET_KEY, remoteFilePath, bucketName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != sign)
        {
            HashMap<String, String> updateData = new HashMap<>();
            updateData.put("sign",sign);
            json.put("message","sign success");
            json.put("code",-1);
            json.put("data",updateData);
        }
        else
        {
            json.put("success","sign fail");
            json.put("code",100);

        }
        return json.toString();
    }

    /**
     * 获取签名信息
     * @param remoteFilePath 上传路径
     * @return 签名信息
     */
    @ResponseBody
    @RequestMapping(value = "getAppSignatureNullParam")
    public String getAppSignatureNullParam(String remoteFilePath) {
        if(StringUtils.isBlank(remoteFilePath))
        {
            remoteFilePath = remotePath;
        }
        JSONObject json = new JSONObject();
        String sign = null;
        try {
            sign = Sign.appSignatureOnce(APP_ID, null, null, remoteFilePath, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != sign)
        {
            HashMap<String, String> updateData = new HashMap<>();
            updateData.put("sign",sign);
//			json.setSuccess(true);
//			json.put("data", sign);
//			json.setMsg("获取成功");
            json.put("message","sign success");
            json.put("code",-1);
            json.put("data",updateData);
        }
        else
        {
//			json.setSuccess(false);
//			json.setMsg("操作错误");
            json.put("success","sign fail");
            json.put("code",100);

        }
        return json.toString();
    }


}
